package com.jbl.te.controller;


import com.jbl.commonutils.MD5;
import com.jbl.commonutils.R;
import com.jbl.te.entity.User;
import com.jbl.te.entity.vo.PassWordVO;
import com.jbl.te.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/te/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/changePwd")
    public R changePwd(@RequestBody PassWordVO passWordVO) {
        User user = userService.getById(passWordVO.getId());
        if(user == null)
            return R.error().message("用户不存在！");
        if(!(passWordVO.getConfirmPwd().equals(passWordVO.getNewPwd())))
            return R.error().message("新密码和确认密码不一致！");
        if( !(user.getPassword().equals(MD5.encrypt(passWordVO.getCurPwd()))) )
            return R.error().message("当前密码不正确！");

        user.setPassword(MD5.encrypt(passWordVO.getNewPwd()));
        boolean update = userService.updateById(user);
        if(update) return R.ok();
        return R.error().message("修改失败！");
    }

}

